﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Lion.WeiXin.Core.Data
{
   public class CmsMenuDAO:ICmsMenu
    {
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int AID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_CMSMenu");
            strSql.Append(" where AID=@AID");
            SqlParameter[] parameters = {
					new SqlParameter("@AID", SqlDbType.Int,4)
			};
            parameters[0].Value = AID;

            return SqlHelper.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Models.CmsMenuModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_CMSMenu(");
            strSql.Append("Code,Title,Url,Sort,IsShow)");
            strSql.Append(" values (");
            strSql.Append("@Code,@Title,@Url,@Sort,@IsShow)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,12),
					new SqlParameter("@Title", SqlDbType.VarChar,100),
					new SqlParameter("@Url", SqlDbType.VarChar,255),
					new SqlParameter("@Sort", SqlDbType.Int,4),
                    new SqlParameter("@IsShow", SqlDbType.Int,4)};
            parameters[0].Value = model.Code;
            parameters[1].Value = model.Title;
            parameters[2].Value = model.Url;
            parameters[3].Value = model.Sort;
            parameters[4].Value = model.IsShow;

            object obj = SqlHelper.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Models.CmsMenuModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_CMSMenu set ");
            strSql.Append("Code=@Code,");
            strSql.Append("Title=@Title,");
            strSql.Append("Url=@Url,");
            strSql.Append("Sort=@Sort,");
            strSql.Append("IsShow=@IsShow ");
            strSql.Append(" where AID=@AID");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,12),
					new SqlParameter("@Title", SqlDbType.VarChar,100),
					new SqlParameter("@Url", SqlDbType.VarChar,255),
					new SqlParameter("@Sort", SqlDbType.Int,4),
                    new SqlParameter("@IsShow", SqlDbType.Int,4),
					new SqlParameter("@AID", SqlDbType.Int,4)};
            parameters[0].Value = model.Code;
            parameters[1].Value = model.Title;
            parameters[2].Value = model.Url;
            parameters[3].Value = model.Sort;
            parameters[4].Value = model.IsShow;
            parameters[5].Value = model.AID;

            int rows = SqlHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int AID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_CMSMenu ");
            strSql.Append(" where AID=@AID");
            SqlParameter[] parameters = {
					new SqlParameter("@AID", SqlDbType.Int,4)
			};
            parameters[0].Value = AID;

            int rows = SqlHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string AIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_CMSMenu ");
            strSql.Append(" where AID in (" + AIDlist + ")  ");
            int rows = SqlHelper.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.CmsMenuModel GetModel(int AID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 AID,Code,Title,Url,Sort,IsShow from T_CMSMenu ");
            strSql.Append(" where AID=@AID");
            SqlParameter[] parameters = {
					new SqlParameter("@AID", SqlDbType.Int,4)
			};
            parameters[0].Value = AID;

            Models.CmsMenuModel model = new Models.CmsMenuModel();
            DataSet ds = SqlHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return ConvertToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.CmsMenuModel ConvertToModel(DataRow row)
        {
            return ConvertToModel(row, 0);
        }

        public Models.CmsMenuModel ConvertToModel(DataRow row, int roleID)
        {
            Models.CmsMenuModel model = new Models.CmsMenuModel();
            if (row != null)
            {
                if (row["AID"] != null && row["AID"].ToString() != "")
                {
                    model.AID = int.Parse(row["AID"].ToString());
                }
                if (row["Code"] != null)
                {
                    model.Code = row["Code"].ToString();
                }
                if (row["Title"] != null)
                {
                    model.Title = row["Title"].ToString();
                }
                if (row["Url"] != null)
                {
                    model.Url = row["Url"].ToString();
                }
                if (row["Sort"] != null && row["Sort"].ToString() != "")
                {
                    model.Sort = int.Parse(row["Sort"].ToString());
                }
                if (row["IsShow"] != null && row["IsShow"].ToString() != "")
                {
                    model.IsShow = int.Parse(row["IsShow"].ToString());
                }
                if (row.Table.Columns.Contains("IsCurrentRole"))
                {
                    if (!(row["IsCurrentRole"] is DBNull)&&int.Parse(row["Role_ID"].ToString())==roleID)
                    {
                        model.IsCurrentRole = true;
                    }
                    else
                    {                        
                       model.IsCurrentRole = false;
                      
                    }
                }
            }
            return model;
        }

        protected List<Models.CmsMenuModel> ConvertToList(DataSet ds)
        {
            return ConvertToList(ds, 0);
        }

        protected List<Models.CmsMenuModel> ConvertToList(DataSet ds,int roleID)
        {
            List<Models.CmsMenuModel> list = new List<Models.CmsMenuModel>();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                Models.CmsMenuModel model = ConvertToModel(row,roleID);
                list.Add(model);
            }
            return list;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Models.CmsMenuModel> GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select AID,Code,Title,Url,Sort,IsShow ");
            strSql.Append(" FROM T_CMSMenu ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            DataSet ds = SqlHelper.Query(strSql.ToString());
            return ConvertToList(ds);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public List<Models.CmsMenuModel> GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" AID,Code,Title,Url,Sort,IsShow ");
            strSql.Append(" FROM T_CMSMenu ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            DataSet ds = SqlHelper.Query(strSql.ToString());
            return ConvertToList(ds);
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM T_CMSMenu ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = SqlHelper.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public List<Models.CmsMenuModel> GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.AID desc");
            }
            strSql.Append(")AS Row, T.*  from T_CMSMenu T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            DataSet ds = SqlHelper.Query(strSql.ToString());
            return ConvertToList(ds);
        }

        public List<Models.CmsMenuModel> GetMenus(int roleID)
        {
            string sql = "select m.AID,m.Code,m.Title,m.Url,m.Sort,m.IsShow from T_Role_CMSMenu rc inner join T_CMSMenu m on m.AID=rc.Menu_ID where rc.Role_ID=@Role_ID ";
            SqlParameter[] parameters = { new SqlParameter("@Role_ID", SqlDbType.Int) };
            parameters[0].Value = roleID;
            DataSet ds = SqlHelper.Query(sql, parameters);
            return ConvertToList(ds);
        }


        public List<Models.CmsMenuModel> GetRoleHaveMenus(int roleID)
        {
            string sql = @"select m.AID,m.Code,m.Title,m.Url,m.Sort,m.IsShow,rm.AID as IsCurrentRole,rm.Role_ID from T_CMSMenu m
left join  (select m.AID,rc.Role_ID from T_Role_CMSMenu rc inner join T_CMSMenu m on m.AID=rc.Menu_ID where rc.Role_ID=@Role_ID) rm on m.AID = rm.AID";
            SqlParameter[] parameters = { new SqlParameter("@Role_ID", SqlDbType.Int) };
            parameters[0].Value = roleID;
            DataSet ds = SqlHelper.Query(sql, parameters);
            return ConvertToList(ds,roleID);
        }
    }
}
